﻿using System;
using System.IO;

namespace Zjh.PackSerializer.Converters.Base
{
    public class Int16Converter : PackConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType.Equals(typeof(short));
        }

        public override bool Read(Stream stream, Type objectType, IPackSerializer serializer, out object output)
        {
            var buffer = new byte[2];
            stream.Read(buffer, 0, buffer.Length);
            output = BitConverter.ToInt16(buffer, 0);
            return true;
        }

        public override bool Write(Stream stream, Type objectType, object input, IPackSerializer serializer)
        {
            var buffer = BitConverter.GetBytes((short)input);
            stream.Write(buffer, 0, buffer.Length);
            return true;
        }
    }
}